<?php

namespace App\Http\Controllers\login;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    public function writeJson($error_code = 0, $data = [], $msg = 'success', $code = 200)
    {
        return response()->json(['error_code' => $error_code, 'data' => $data, 'msg' => $msg], $code);
    }

    /**
     * 递归处理层级
     * @return void
     */
    public function sonLevel($data, $pid = 0)
    {
        $arr = [];
        foreach ($data as $key => $val) {
            //等于0的时候代表是顶级菜单
            if ($val['pid'] == $pid) {
                //递归处理子集
                $val['son'] = $this->sonLevel($data, $val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }
}
